﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Models;
using BLL;
using Newtonsoft.Json.Linq;

namespace GoShopping.tools
{
    /// <summary>
    /// Reviewds 的摘要说明
    /// </summary>
    public class Reviewds : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string t = context.Request["t"].ToString();
            //0:增加评论
            //1:查询评论，根据goodsID
            //2:联表查询 ReView_UserInfo
            switch (t)
            {
                case "0":
                    add(context);
                    break;
                case "1":
                    getALL(context);
                    break;
                case "2":
                    getUser_Re(context);
                    break;

            }
        }
        //联表查询 ReView_UserInfo
        private void getUser_Re(HttpContext context)
        {
            int GoodsID = Convert.ToInt32(context.Request["GoodsID"]);
            List<ReView_UserInfo> list = ReViewBLL.getUser_ReBLL(GoodsID);
            //把数据转换为JSON格式
            JObject obj = new JObject();
            JArray array = JArray.FromObject(list);
            obj.Add("res", array);
            if (list.Count > 0)
            {

                context.Response.Write(obj);
            }
            else
            {
                context.Response.Write("{\"status\":\"查询不到评论！\"}");
            }
        }

        private void getALL(HttpContext context)
        {
            int GoodsID = Convert.ToInt32(context.Request["GoodsID"]);
            List<ReView> list=ReViewBLL.getAllBLL(GoodsID);
            //把数据转换为JSON格式
            JObject obj = new JObject();
            JArray array = JArray.FromObject(list);
            obj.Add("res", array);
            if (list.Count > 0)
            {

                context.Response.Write(obj);
            }
            else
            {
                context.Response.Write("{\"status\":\"查询不到评论！\"}");
            }
        }

        //增加评论
        private void add(HttpContext context)
        {
            ReView reView = new ReView();
            reView.UserID = Convert.ToInt32(context.Request["UserID"]);
            reView.GoodsID= Convert.ToInt32(context.Request["GoodsID"]);
            reView.ReviewContent= Convert.ToString(context.Request["Content"]);
            reView.Manyi= Convert.ToInt32(context.Request["Manyi"]);
            bool res=ReViewBLL.addBLL(reView);
            if (res)
            {
                context.Response.Write("{\"status\":\"评论成功！\"}");
            }
            else
            {
                context.Response.Write("{\"status\":\"评论失败！\"}");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}